/**
 * 文  件  名：  ShuoShuoDaoImpl.java
 * 工  程  名：  7You
 * 创建日期：  2013-10-14
 * 创建作者：  杨  强
 */
package com.qiyou.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.dao.IShuoShuoDao;
import com.qiyou.entity.ShuoShuo;
import com.qiyou.util.DBUtil;
import com.qiyou.util.TransformUtil;

/**
 * 说说数据操作实现类
 * 
 * @author 杨 强
 * 
 */
public class ShuoShuoDaoImpl implements IShuoShuoDao {
    private Connection conn = null;

    public ShuoShuoDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(ShuoShuo shuoShuo) throws SQLException {
        boolean result = false;
        String sql = "insert into `tb_shuoshuo`(`userID`, `ssDate`, `ssContent`) values(?, ?, ?)";
        Object[] params = TransformUtil.beanTOArray(shuoShuo, true);
        params[1] = new Timestamp(shuoShuo.getDate().getTime());
        result = DBUtil.execute(conn, sql, params);
        return result;
    }

    @Override
    public boolean doUpdate(ShuoShuo shuoShuo) throws SQLException {
        boolean result = false;
        String sql = "update `tb_shuoshuo` set `ssID` = ?, `userID` = ?, `ssDate` = ?, `ssContent` = ? where `ssID` = ?";
        Object[] params = TransformUtil.beanTOArray(shuoShuo, false);
        params[2] = new Timestamp(shuoShuo.getDate().getTime());
        result = DBUtil.execute(conn, sql, params, params[0]);
        return result;
    }

    @Override
    public boolean doDelete(long shuoShuoID) throws SQLException {
        boolean result = false;
        String sql = "delete from `tb_shuoshuo` where `ssID` = ?";
        result = DBUtil.execute(conn, sql, shuoShuoID);
        return result;
    }

    @Override
    public ShuoShuo getShuoShuo(long shuoShuoID) throws SQLException {
        ShuoShuo result = null;
        String sql = "select * from `tb_shuoshuo` where `ssID` = ?";
        List<Object[]> results = DBUtil.query(conn, sql, shuoShuoID);
        if (null != results && results.size() > 0) {
            result = (ShuoShuo) TransformUtil.arrayToBean(results.get(0),
                    ShuoShuo.class);
        }
        return result;
    }

    @Override
    public List<ShuoShuo> getShuoShuos(String userID) throws SQLException {
        List<ShuoShuo> result = null;
        String sql = "select * from `tb_shuoshuo` where `userID` = ?";
        List<Object[]> results = DBUtil.query(conn, sql, userID);
        if (null != results && results.size() > 0) {
            result = new ArrayList<ShuoShuo>(results.size());
            ShuoShuo shuoShuo = null;
            for (Object[] objects : results) {
                shuoShuo = (ShuoShuo) TransformUtil.arrayToBean(objects,
                        ShuoShuo.class);
                result.add(shuoShuo);
            }
        }
        return result;
    }

    @Override
    public List<ShuoShuo> getShuoShuos(Object... params) throws SQLException {
        List<ShuoShuo> result = null;
        StringBuffer sql = new StringBuffer("select * from `tb_shuoshuo` ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        List<Object[]> all = DBUtil.query(conn, sql.toString());
        if (null != all && all.size() > 0) {
            result = new ArrayList<ShuoShuo>(all.size());

            for (Object[] objects : all) {
                ShuoShuo shuoShuo = (ShuoShuo) TransformUtil.arrayToBean(
                        objects, ShuoShuo.class);
                result.add(shuoShuo);
            }
        }

        return result;
    }

}
